package com.joplus.housekeeper.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.TextView;

import com.joplus.housekeeper.utils.L;


/**
 * Created by Administrator on 2016/7/13.
 */
public class HorizontalRecyleViewWithExpend extends ViewGroup {


    private final String TURNPAGE = "松" + "\n" + "手" + "\n" + "跳" + "\n" + "转";
    private final String CONTIUNE = "向" + "\n" + "右" + "\n" + "滑" + "\n" + "动" + "\n" + "跳" + "\n" + "转";
    private Boolean isShowExpend = false;//是否是最后一个

    private int criticalPix = 50;//滑动超过的临界值
    private int textviewWidth = 100;

    OnExpend isOnExpend;//临界扩展的回调

    float lastTouchX;
    float lastTouchY;


    Context context;
    RecyclerView mRecyclerView;
    Scroller scroller;
    TextView tview;

    RecyclerView.LayoutManager lm;

    int mFristScollerX;

    private Boolean isOverLimit = true;

    //
//    public  abstract  BaseAdapter setAdapter();
    int scorll;

    public HorizontalRecyleViewWithExpend(Context context) {
        super(context);
        init(context);
    }

    public HorizontalRecyleViewWithExpend(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public HorizontalRecyleViewWithExpend(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public void setOnExpend(OnExpend isOnExpend) {
        this.isOnExpend = isOnExpend;
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            if (isOnExpend != null)
                isOnExpend.touchExpend();
            super.handleMessage(msg);
        }
    };

    public RecyclerView getmRecyclerView() {
        return mRecyclerView;
    }


    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);

            if (i == 0) {
                child.layout(getMeasuredWidth() + textviewWidth * 2, 0, getMeasuredWidth() + textviewWidth * 3, getHeight());
            } else {
                child.layout(0, 0, getMeasuredWidth(), getHeight());
            }
        }

    }


    private void init(Context context) {
        this.context = context;
        scroller = new Scroller(context);
        mRecyclerView = new RecyclerView(context);
        tview = new TextView(context);
        //tview.setText(CONTIUNE);
        tview.setWidth(textviewWidth);
        tview.setHeight(LayoutParams.MATCH_PARENT);
        tview.setGravity(Gravity.CENTER);
        tview.setGravity(Gravity.CENTER_VERTICAL);
        tview.setTextColor(Color.BLACK);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getWidth() - textviewWidth, LayoutParams.MATCH_PARENT);
        mRecyclerView.setLayoutParams(params);
        lm = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(lm);
        mRecyclerView.addItemDecoration(new InsetDecoration(context));

        addView(tview);
        addView(mRecyclerView);

    }


    /**
     * 在最后临界值且为继续向左滑动屏蔽滑动事件
     *
     * @param e
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        final int action = MotionEventCompat.getActionMasked(e);
        Log.d("onInterceptTouchEvent", e.getAction() + "");
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                lastTouchX = e.getRawX();
                lastTouchY = e.getRawY();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                return false;
            case MotionEvent.ACTION_MOVE:
                Log.d("onInterceptTouchEvent", "x" + e.getRawX() + " " + "y" + e.getRawY());
                if (!mRecyclerView.canScrollHorizontally(1)) {
                    if (lastTouchX > e.getRawX()) {
                        lastTouchX = e.getRawX();
                        mRecyclerView.setFocusable(false);
                        return true;
                    }

                }

        }
        return false;
    }


    @Override
    public boolean onTouchEvent(MotionEvent e) {



        switch (e.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float d = e.getRawX() - lastTouchX;
                if(Math.abs(e.getRawX()-lastTouchX)<textviewWidth * 2||(lastTouchX-e.getRawX())<textviewWidth * 4){
                    tview.setText(CONTIUNE);

                }else if((lastTouchX-e.getRawX())>=textviewWidth * 4){
                    tview.setText(TURNPAGE);
                }
                getParent().requestDisallowInterceptTouchEvent(true);//屏蔽父控件拦截onTouch事件
                if (d < 0) {
                    if((lastTouchX-e.getRawX())<textviewWidth * 4){
                        scrollTo((int) Math.abs(d), 0);
                        scorll = getScrollX();
                    }

                } else {
                    isShowExpend = true;
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
            case MotionEvent.ACTION_UP:
                if((lastTouchX-e.getRawX())>=textviewWidth * 4){
                    scrollTo(0, 0);
                    handler.sendEmptyMessage(0);
                    isShowExpend = false;
                }else{
                    scrollTo(0, 0);

                }
                break;

        }
        return true;
    }


    /**
     * 是否达到显示扩展的临界回调
     */
    public static interface OnExpend {
        public void touchExpend();
    }


}
